<template>
  <el-dialog title="添加权限" :visible.sync="isShowRole" width="40%">
    <el-checkbox-group v-model="dataList">
      <el-checkbox :label="item.id" v-for="item in roleList" :key="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="exit">取 消</el-button>
      <el-button type="primary" @click="submitRole">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getUserPhoto } from "@/api/user";
import { assignRoles } from "@/api/employees";
import { getRoleList } from "@/api/setting.js";
export default {
  props: {
    isShowRole: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: String,
      default: null,
    },
  },
  created() {
    this.getRole();
  },
  data() {
    return {
      dataList: [], // 已经有的权限
      roleList: [], // 全部权限
      addRole: [], // 添加的权限
    };
  },
  methods: {
    exit() {
      this.$emit("update:isShowRole", false);
    },
    async getRole() {
      const { rows } = await getRoleList();
      this.roleList = rows;
    },
    async getList(id) {
      const { roleIds } = await getUserPhoto(id);
      this.dataList = roleIds;
    },
    // 确定添加权限
    async submitRole() {
      try {
        const data = await assignRoles({
          id: this.userId,
          roleIds: this.dataList,
        });
        this.$message({
          type: "success",
          message: "修改成功",
        });
      } catch (error) {}
      this.exit();
    },
  },
};
</script>

<style>
</style>
